<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>05员工表格练习</title>
</head>
<body>
<input type="text" placeholder="请输入员工姓名" id="i1">
<input type="text" placeholder="请输入员工工资" id="i2">
<input type="text" placeholder="请输入员工岗位" id="i3">
<button onclick="addEmp()">添加员工</button>
<hr>
<table border="1px">
  <caption>员工表</caption>
  <tr>
    <th>姓名</th>
    <th>工资</th>
    <th>岗位</th>
  </tr>
</table>
<script>
  //准备一个空数组,用来保存所有的员工数据
  let arr = [];
  //1.每次点击添加员工按钮,调用此函数
  function addEmp(){
    //2.获取输入框中输入的三个值
    let uName = document.getElementById('i1').value;
    let uSalary = document.getElementById('i2').value;
    let uJob = document.getElementById('i3').value;
    //3.创建1个tr与3个td,都是空的!
    let tr = document.createElement('tr');//<tr></tr>
    let nameTd = document.createElement('td');//<td></td>
    let salaryTd = document.createElement('td');//<td></td>
    let jobTd = document.createElement('td');//<td></td>
    //4.把收集到输入框数据装入td中
    nameTd.innerHTML = uName;
    salaryTd.innerHTML = uSalary;
    jobTd.innerHTML = uJob;
    //5.将3个准备好的td装入tr中
    tr.append(nameTd,salaryTd,jobTd);
    //6.获取表格元素,将准备好的tr装入table中
    let table = document.querySelector('table');
    table.append(tr);
    //7.表格显示成功后,要清空输入框中的数据
    document.getElementById('i1').value="";
    document.getElementById('i2').value="";
    document.getElementById('i3').value="";
    //8.2将本次收集到的员工数据存入员工数组里(后期可以给后端发请求)
    arr.push({name:uName,salary:uSalary,job:uJob});
    console.log(arr);
  }
</script>
</body>
</html>